package com.example.demo.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;


@Controller
public class HelloController {

	@RequestMapping("/")
	public String hello(Model model) {
	    Date date = new Date();
	    model.addAttribute("date", date); 
	    System.out.println("initDate = " + date.getTime());
		return "test";
	}
    
    @ResponseBody
    @PostMapping("/time")
    public JSONObject test(@RequestParam("date")String dateStr, HttpServletRequest request) throws ParseException{
        JSONObject jsonObject = new JSONObject();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
//        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
//        Date date = new Date();
//        System.out.println(dateFormat.format(date));
//        System.out.println(dateFormat.getTimeZone().getID());
//        jsonObject.put("dateTime", date);   
        
        
//        Enumeration<String> names = request.getHeaderNames();
//        while (names.hasMoreElements()) {
//            System.out.println(names.nextElement());           
//        }
        
        System.out.println("dateStr = " + dateStr);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        System.out.println("initDate = " + dateFormat.parse(dateStr).getTime());
        //SimpleDateFormat dateFormat = new SimpleDateFormat();
        
//        String localDateString = dateFormat.format(date);
       
//        System.out.println("localDateString = " + localDateString);
//        System.out.println("request.getLocale().getCountry() = " + request.getLocale().getCountry());
//        System.out.println("request.getLocale().getDisplayCountry() = " + request.getLocale().getDisplayCountry());
//        System.out.println("request.getLocale().getDisplayName() = " + request.getLocale().getDisplayName());
//        System.out.println("long number = " + date.getTime());
        
        
        jsonObject.put("dateTime", dateFormat.parse(dateStr)); 

        return jsonObject;
    }
    
    @ResponseBody
    @PostMapping("/getTime")
    public JSONObject getDate(@RequestParam("submittedDate")String dateStr) throws ParseException {
        System.out.println("==================================================================================");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        System.out.println("dateStr = " + dateStr);
        Date date = dateFormat.parse(dateStr);
        System.out.println("submittedDate = " + date);
        System.out.println("submittedDate number = " + date.getTime());
//        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
//        String submittedDateString = dateFormat.format(date);
//        System.out.println("submittedDateString = " + submittedDateString);
//        
//        
//        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
//        System.out.println("submittedDateString = " + submittedDateString);
//        System.out.println("Changed submittedDate = " + dateFormat.parse(submittedDateString));
//        System.out.println("shit");
        
        return new JSONObject();
    }
}
